<?PHP
session_start();
if (!isset($swKernCMS)) {
	$swKernCMS = 1;
	function GotoURL($URL){
		echo '<script language="JavaScript">' .
				' window.document.location.href="'.$URL.'" ' .
				'</script> ';
		exit;
	}
	// Sparche
	function xml_join($root, $append) {
		if ($append) {
			if (strlen(trim((string) $append)) == 0) {
				$xml = $root->addChild($append->getName());
				foreach ($append->children() as $child) {
					xml_join($xml, $child);
				}
			} else {
				$xml = $root->addChild($append->getName(), (string) $append);
			}
			foreach ($append->attributes() as $n => $v) {
				$xml->addAttribute($n, $v);
			}
		}
	}

	if (!empty ($_POST["lan"])) {
		$_Lan = $_POST["lan"];
	} else
		if (isset ($_SESSION["_lan"])) {
		$_Lan = $_SESSION["_lan"];
	} else {
		$_Lan = "en-us";
	}
	$_SESSION["_lan"] = $_Lan;
	if (file_exists("lange/$_Lan.xml")) {
		$_LanXml = simplexml_load_file("lange/$_Lan.xml");
	}

	function LangText($Key) {
		echo LangTextOut($Key);
	}
	function LangTextOut($Key) {
		$Key = str_replace(' ','',$Key);
		global $_LanXml;
		global $_Lan;
		if ($_LanXml != NULL) {
			$p_cnt = count($_LanXml->phrase);
			for ($i = 0; $i < $p_cnt; $i++) {
				if ($Key == $_LanXml->phrase[$i]["id"][0]) {
					return utf8_decode($_LanXml->phrase[$i]["value"][0]);
				}
			}
		}
		$xmlHandle = fopen("lange/$_Lan.xml", "w");
		$rating = $_LanXml->addChild('phrase');
		$rating->addAttribute('id', $Key);
		$rating->addAttribute('value', $Key);
		fwrite($xmlHandle, $_LanXml->asXML());
		fclose($xmlHandle);
		return $Key;
	}
	function SearchTextLanguage($input) {
		$regex = '/\$\[(?P<name>\w+)\]/';
		preg_match_all($regex, $input, $match);
		foreach ($match[1] as $value) {
			$input = str_replace('$[' . $value . ']', LangTextOut($value), $input);
		}
		return $input;
	}
	//Sparache	 end
	function getDataFild($fildname, $swEdit, $data) {
		if ($swEdit == 1) {
			echo $data[$fildname];
		}
	}
	function geFile($fileID, $class = "smallPic", $subAdresse = "", $width = 200, $height = 200) {
		$sql = "select * from files where id=$fileID";
		global $_db_link;
		$_rd = mysql_query($sql, $_db_link);
		if ($_rd) {
			if ($daten = mysql_fetch_array($_rd, MYSQL_ASSOC)) {
				return "<img class='" . $class . "' src='Admin/UploadFiles/" . $subAdresse . $daten["Adresse"] . "' width=$width height=$height   />";
			}
		}
	}
	function getListBox(& $List, $selectID, $table, $where, $Titel = "Titel") {
		if ($selectID == NULL || $selectID == 0) {
			$List = $List . '<option value="" selected="selected">Please Select</option>';
		} else {
			$List = $List . '<option value="">Please Select</option>';
		}
		$sql = "select * FROM $table $where";
		global $_db_link;
		$_rd = mysql_query($sql, $_db_link);
		if ($_rd) {
			while ($datenLogo = mysql_fetch_array($_rd, MYSQL_ASSOC)) {
				if ($selectID == $datenLogo["ID"]) {
					$select = 'selected="selected"';
				} else {
					$select = "";
				}
				$List = $List . "<option value='" . $datenLogo["ID"] . "' $select>" . $datenLogo[$Titel] . "</option>";
			}
		}
	}
	function uploadFile() {

		if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 1024 * 1000)) {
			if ($_FILES["file"]["error"] > 0) {
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			} else {
				if (file_exists("Admin/UploadFiles/" . $_FILES["file"]["name"])) {
					echo $_FILES["file"]["name"] . " already exists. ";
				} else {
					move_uploaded_file($_FILES["file"]["tmp_name"], "Admin/UploadFiles/" . $_FILES["file"]["name"]);
					$sql = "insert INTO  files (Titel,Adresse,UserID)
					values('" . $_FILES["file"]["name"] . "','" . $_FILES["file"]["name"] . "'," . $_SESSION["CMS_User"]["ID"] . ")";
					global $_db_link;
					$_rd = mysql_query($sql, $_db_link);
					global $LogoID;
					$LogoID = mysql_insert_id();
					include ('SimpleImage.php');
					$image = new SimpleImage();
					$image->load("Admin/UploadFiles/" . $_FILES["file"]["name"]);
					$image->resizeToWidth(150);
					$image->save("Admin/UploadFiles/medium/" . $_FILES["file"]["name"]);
					$image->resize(75, 75);
					$image->save("Admin/UploadFiles/small/" . $_FILES["file"]["name"]);
					return $LogoID;
				}
			}
		} else {
			echo "Invalid file";
		}

	}
	function getOption(& $datakategorie, $selectID, $ParentID = "", $level = 0) {
		$strlevel = "";
		for ($i = 0; $i < $level; $i++) {
			$strlevel = $strlevel . "---";
		}
		$strOut = "";
		foreach ($datakategorie as $index => $data) {
			if ($data["ParentID"] == $ParentID) {
				if ($data["ID"] == $selectID) {
					$select = "selected='selected'";
				} else {
					$select = "";
				}
				$strOut = $strOut . "<option value='" . $data["ID"] . "' $select>" . $strlevel . $data["Titel"] . "</option>";
				$strOut = $strOut . getOption(& $datakategorie, $selectID, $data["ID"], $level +1);
			}
		}
		return $strOut;
	}
	function CreatPaging($table, $pageN, $class, $rowPage = 10, $where = "") {
		$sql = "select count(ID) from $table $where ";
		global $_db_link;
		$_rd = mysql_query($sql, $_db_link);
		if ($_rd) {
			$count = mysql_result($_rd, 0);
			$pageSum = ceil($count / $rowPage);
			$str = "";
			global $tagID;
			global $q;
			if ($pageN != 0) {
				$str = $str . "<a href='index.php?tag=$tagID&pageN=$pageN&q=$q'>&lt;" . LangTextOut("Zurück") . "</a> | ";
			}
			for ($i = 1; $i <= $pageSum; $i++) {
				if ($i != 1) {
					$str = $str . " - ";
				}
				if ($i != $pageN +1) {
					$str = $str . "<a href='index.php?tag=$tagID&pageN=$i&q=$q'>$i</a>";
				} else {
					$str = $str . $i;
				}
			}
			if ($pageN +1 != $pageSum  && $count > $rowPage) {
				$pageN += 2;
				$str = $str . " | <a href='index.php?tag=$tagID&pageN=$pageN&q=$q'>Weiter&gt;</a>";
			}
			return $str;
		}
	}
}
?>
